package com.jhd.fuzhou.view.punishment;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.jhd.fuzhou.view.login.BaseActivity;
import com.jhd.fuzhou.R;
import com.jhd.fuzhou.service.PunishmentService;
import com.jhd.fuzhou.util.CustomToast;
import com.jhd.fuzhou.util.StringShow;
import com.jhd.fuzhou.util.UtilHelper;
import com.jhd.fuzhou.vo.AdministrativeIllegalVo;
import com.jhd.fuzhou.vo.JsonForResult;

import net.zigin.commons.util.ParamsMapUtil;
import net.zigin.commons.util.StringUtil;

import java.util.List;
import java.util.Map;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class AdministrativeIllegalActivity extends BaseActivity {

    private Context context=this;
    @BindView(R.id.recyclerView) RecyclerView recyclerView;
    private List<AdministrativeIllegalVo> administrativeIllegalVoList;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_administrative_illegal);
    }

    @Override
    protected void processLogic() {
        onBack();
        String id=getIntent().getStringExtra("id");
        getAdministrativeIllegal(id);
    }

    private void getAdministrativeIllegal(String id) {
        Retrofit retrofit = UtilHelper.retrofit(application, UtilHelper.baseUrls.API());//URL的头
        PunishmentService vs = retrofit.create(PunishmentService.class);
        Map<String, String> params = ParamsMapUtil.getInstance("punishmentId",id);
        Call<JsonForResult> jsonForResultCall = vs.getAdministrativeIllegal(application.getAccessToken(),params);//调用接口获取json返回
        jsonForResultCall.enqueue(new Callback<JsonForResult>() {
            @Override
            public void onResponse(Call<JsonForResult> call, Response<JsonForResult> response) {
                try {
                    JsonForResult jsonForResult = (JsonForResult) response.body();
                    if (jsonForResult != null && jsonForResult.isSuccess()) {
                        if(jsonForResult.getData()!=null){
                            administrativeIllegalVoList=JSON.parseArray(jsonForResult.getData().toString(), AdministrativeIllegalVo.class);
                            LinearLayoutManager linearLayoutManager=new LinearLayoutManager(context);
                            recyclerView.setLayoutManager(linearLayoutManager);
                            IllegalAdapter adapter=new IllegalAdapter();
                            recyclerView.setAdapter(adapter);
                        }else{
                            CustomToast.showText(context,"该案件暂无执法依据！",3000,0,0).show();
                        }
                    } else {
                        UtilHelper.alertMsg(context, "错误", jsonForResult.getMessage());
                    }
                } catch (Exception e) {
//                    CustomToast.showText(context,e.getLocalizedMessage(),3000,0,0).show();
                }
            }

            @Override
            public void onFailure(Call<JsonForResult> call, Throwable t) {
                String msg = "网络异常!";
                if (StringUtil.isNullOrEmpty(t.getMessage()) == false) msg = msg;
                UtilHelper.alertMsg(context, "错误", msg);
            }
        });
    }
    public  class IllegalAdapter extends RecyclerView.Adapter<IllegalAdapter.ViewHolder> {
        @Override
        public IllegalAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_law_basis, parent, false);
            IllegalAdapter.ViewHolder viewHolder = new IllegalAdapter.ViewHolder(itemView);
            return viewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull IllegalAdapter.ViewHolder holder, int position) {
            holder.tv_count.setText("执法依据("+(position+1)+")");
            holder.tv_illegal_content.setText(StringShow.showStr(administrativeIllegalVoList.get(position).getContent()));
            holder.tv_illegal_basis.setText(StringShow.showStr(administrativeIllegalVoList.get(position).getClause()));
            holder.tv_punishment_basis.setText(StringShow.showStr(administrativeIllegalVoList.get(position).getGist()));
        }

        @Override
        public int getItemCount() {
            return administrativeIllegalVoList.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder {
            TextView tv_count,tv_illegal_content,tv_illegal_basis,tv_punishment_basis;
            LinearLayout linearLayout;
            public ViewHolder(View itemView) {
                super(itemView);
                this.tv_count = itemView.findViewById(R.id.tv_count);
                this.tv_illegal_content = itemView.findViewById(R.id.tv_illegal_content);
                this.tv_illegal_basis = itemView.findViewById(R.id.tv_illegal_basis);
                this.tv_punishment_basis = itemView.findViewById(R.id.tv_punishment_basis);
            }
        }
    }
}